package helloWorld;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import json.Mapper;

import model.Project;
import model.User;

import org.mt4j.MTApplication;

import com.fasterxml.jackson.core.JsonProcessingException;

public class StartHelloWorld extends MTApplication {

	private static final long serialVersionUID = -6958040816948135095L;

	public static void main(String[] args) {
		initialize();

		List<C> cList = new ArrayList<C>();
		cList.add(new C());
		cList.add(new C());
		cList.add(new C());
		cList.add(new C());
		Set<C> cSet = new HashSet<C>();
		cSet.add(new C());
		cSet.add(new C());
		cSet.add(new C());
		Map<Long, C> cMap = new HashMap<Long, C>();
		cMap.put(1l, new C());
		cMap.put(2l, new C());
		cMap.put(3l, new C());
		A a = new A(new B(cList, cSet, cMap));
		String result;
		try {
			result = Mapper.getObjectMapper().writeValueAsString(a);
			System.out.println(result);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		User creator = new User("Arnaud");
		Project project = new Project(5l, "test project", "", creator);
		Project project2 = null;
		JAXBContext context = null;
		Marshaller m = null;
		StringWriter writer = new StringWriter();
		String xml = null;
		try {
			context = JAXBContext.newInstance(project.getClass());
			m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

			// Write to System.out
			m.marshal(project, System.out);
			m.marshal(project, writer);
			xml = writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			StringReader reader = new StringReader(xml);

			Unmarshaller um = context.createUnmarshaller();

			project2 = (Project) um.unmarshal(reader);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			context = JAXBContext.newInstance(project.getClass());
			m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

			// Write to System.out
			m.marshal(project2, System.out);
			xml = writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void startUp() {
		addScene(new HelloWorldScene(this, "Hello World Scene"));
	}
}
